package com.kedacom.demo.springrabbitex.rpc.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kedacom.demo.springrabbitex.RabbitMessageSender;
import com.kedacom.demo.springrabbitex.rpc.Message;
import com.kedacom.demo.springrabbitex.rpc.ResponseMessage;

public class RPCClient {

	private static Logger logger = LoggerFactory.getLogger(RPCClient.class);

	public static void main(String[] args) throws InterruptedException {
		Runner runner1 = new Runner();
		runner1.start = 1;
		runner1.count = 100;
		new Thread(runner1).start();

		// Runner runner2 = new Runner();
		// runner2.start = 1001;
		// runner2.count = 100;
		// new Thread(runner2).start();

		Thread.sleep(10000);
		System.exit(1);
	}

	static class Runner implements Runnable {

		ClassPathXmlApplicationContext context;
		RabbitMessageSender rabbitMessageSender;
		int start;
		int count;

		public Runner() {
			context = new ClassPathXmlApplicationContext("/springrabbitex/spring-rabbitmq-rpc-client.xml");
			rabbitMessageSender = context.getBean(RabbitMessageSender.class);
		}

		public void run() {
			for (int i = start; i < start + count; i++) {
				ResponseMessage response = (ResponseMessage) rabbitMessageSender.convertSendAndReceive(new Message(
						"rpc message " + i));
				logger.debug(response.getBody());
			}
		}
	}

}
